/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/core/{{modules}}/service/impl/Abstract{{entities@MINHERIT}}Service.java.hbs
 */
package cn.ibizlab.core.authentication.service.impl;

import java.util.*;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.util.*;
import cn.ibizlab.util.errors.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Lazy;
import cn.ibizlab.core.authentication.domain.AuthLogin;
import cn.ibizlab.core.authentication.filter.AuthLoginSearchContext;
import cn.ibizlab.core.authentication.service.AuthLoginService;
import cn.ibizlab.core.authentication.mapper.AuthLoginMapper;
import cn.ibizlab.util.enums.Entities;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.ibizlab.core.ad.domain.SysPerson;
import cn.ibizlab.core.ad.service.SysPersonService;

/**
 * 实体[登录信息] 服务对象接口实现（常规多继承虚拟实体）
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractAuthLoginService extends ServiceImpl<AuthLoginMapper,AuthLogin> implements AuthLoginService {

    @Autowired
    @Lazy
    protected SysPersonService sysPersonService;

    @Autowired
    @Lazy
    protected cn.ibizlab.core.authentication.mapping.AuthLoginInheritMapping authLoginInheritMapping;

    protected int batchSize = 500;

    public AuthLogin get(AuthLogin et) {
        AuthLogin rt = this.baseMapper.selectEntity(et);
        if(rt == null)
            throw new NotFoundException("数据不存在",Entities.AUTH_LOGIN.toString(),et.getId());
        rt.copyTo(et,true);
        return et;
    }

    public List<AuthLogin> getByEntities(List<AuthLogin> entities) {
        return this.baseMapper.selectEntities(entities);
    }

    public AuthLogin getDraft(AuthLogin et) {
        return et;
    }

    public Integer checkKey(AuthLogin et) {
        return (!ObjectUtils.isEmpty(et.getId()) && this.count(Wrappers.<AuthLogin>lambdaQuery().eq(AuthLogin::getId, et.getId()))>0)?1:0;
    }

    @Override
    
    public boolean create(AuthLogin et) {
        if(ObjectUtils.isEmpty(et.getId()))
            et.setId((String)et.getDefaultKey(true));
        sysPersonService.create(authLoginInheritMapping.toSysPerson(et));
        get(et);
        return true;
    }

    
    public boolean createBatch(List<AuthLogin> list) {
        list.forEach(et->getSelf().create(et));
        return true;
    }

    
    public boolean update(AuthLogin et) {
        sysPersonService.update(authLoginInheritMapping.toSysPerson(et));
        get(et);
        return true;
    }

    
    public boolean updateBatch(List<AuthLogin> list) {
        list.forEach(et->getSelf().update(et));
        return true;
    }

    @Override
    
    public boolean save(AuthLogin et) {
        if(checkKey(et) > 0)
            return getSelf().update(et);
        else
            return getSelf().create(et);
    }

    
    public boolean saveBatch(List<AuthLogin> list) {
        list.forEach(et->getSelf().save(et));
        return true;
    }

    
    public boolean remove(AuthLogin et) {
        sysPersonService.remove(authLoginInheritMapping.toSysPerson(et));
        return true;
    }

    
    public boolean removeByEntities(List<AuthLogin> entities) {
        for (AuthLogin et : entities)
            if(!getSelf().remove(et))
                return false;
        return true;
    }

    @Override
    public List<JSONObject> select(String sql, Map param){
        return this.baseMapper.selectBySQL(sql,param);
    }

    @Override
    @Transactional
    public boolean execute(String sql , Map param){
        if (sql == null || sql.isEmpty()) {
            return false;
        }
        if (sql.toLowerCase().trim().startsWith(SqlMethod.INSERT_ONE.getMethod())) {
            return this.baseMapper.insertBySQL(sql,param);
        }
        if (sql.toLowerCase().trim().startsWith(SqlMethod.UPDATE.getMethod())) {
            return this.baseMapper.updateBySQL(sql,param);
        }
        if (sql.toLowerCase().trim().startsWith(SqlMethod.DELETE.getMethod())) {
            return this.baseMapper.deleteBySQL(sql,param);
        }
        log.warn("暂未支持的SQL语法");
        return true;
    }

    @Override
    protected Class currentMapperClass() {
        return AuthLoginMapper.class;
    }

    @Override
    protected Class currentModelClass() {
        return AuthLogin.class;
    }
}